package com.blb.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.entity.Comtype;
import com.blb.entity.ComtypeExample;
import com.blb.mapper.ComtypeMapper;
import com.blb.service.IComtypeService;
import com.blb.utils.IconfontUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("comtype")
public class ComTypeController {
	@Autowired
	private IComtypeService comtypeService;
	
	@Autowired
	private ComtypeMapper comtypeMapper;
	
	/**
	 * 跳转商品分类界面
	 * @return
	 */
	@RequestMapping("index")
	public String getIndex(){
		return "admin/comtype/comtype_list";
	}
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping("page")
	public  LayuiPage PageData(int page, int limit,String name){
		return comtypeService.page((page-1)*limit, limit, name);
	}
	
	
	/**
	 * 跳转添加页面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("showAdd")
	public String showAdd(Model model, HttpServletRequest request) throws IOException{
		List<String> iconfonts = IconfontUtils.getIconfonts(request);
		model.addAttribute("iconfonts", iconfonts);
		return "admin/comtype/comtype_add";
	}
	
	/**
	 * 添加
	 */
	@RequestMapping("add")
	@ResponseBody
	public String add(Comtype comtype){
		comtype.setId(UUIDUtils.getID());

		comtypeService.add(comtype);
		
		return "";
	}
	
	@RequestMapping("isExist")
	@ResponseBody
	public String isExist(String name){
		ComtypeExample comtypeExample = new ComtypeExample();
		comtypeExample.createCriteria().andNameEqualTo(name);
		List<Comtype> list = comtypeMapper.selectByExample(comtypeExample);
		if(!list.isEmpty()){
			return "类别名称已存在";
		}
		return "";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String selete(@RequestBody List<String> ids){
		comtypeService.delete(ids);
		return "";
	}
	
	/**
	 * 显示修改页面
	 * @param comtype
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("showUpdate")
	public String showUpdate(Comtype comtype,Model model,HttpServletRequest request) throws IOException{
		List<String> iconfonts = IconfontUtils.getIconfonts(request);
		model.addAttribute("iconfonts", iconfonts);
		model.addAttribute("comtype", comtype);
		return "admin/comtype/comtype_update";
	}
	
	@RequestMapping("update")
	@ResponseBody
	public String update(Comtype comtype){
		comtypeService.update(comtype);
		return "";
	}
}
